package config;

import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import java.io.InputStream;
import java.util.Map;

public class ConfigLoader {
    private static Map<String, Object> config;

    public ConfigLoader(String fileName) {
        Yaml yaml = new Yaml(new Constructor(Map.class));
        try (InputStream in = ConfigLoader.class
                .getClassLoader()
                .getResourceAsStream(fileName)) {
            config = yaml.load(in);
            config = (Map<String, Object>) config.get("app");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getRegistryHost() {
        Map<String, Object> registry = (Map<String, Object>) config.get("registry");
        return (String) registry.get("host");
    }

    public static int getRegistryPort() {
        Map<String, Object> registry = (Map<String, Object>) config.get("registry");
        return (Integer) registry.get("port");
    }
    public static String getServerHost() {
        Map<String, Object> server = (Map<String, Object>) config.get("server");
        return (String) server.get("host");
    }
    public static int getServerPort() {
        Map<String, Object> server = (Map<String, Object>) config.get("server");
        return (Integer) server.get("port");
    }
    public static String getMysqlUrl() {
        Map<String, Object> mysql = (Map<String, Object>) config.get("mysql");
        return (String) mysql.get("url");
    }
    public static String getMysqlUsername(){
        Map<String, Object> mysql = (Map<String, Object>) config.get("mysql");
        return (String) mysql.get("username");
    }
    public static String getMysqlPassword(){
        Map<String, Object> mysql = (Map<String, Object>) config.get("mysql");
        return (String) mysql.get("password");
    }
}

